define(['jquery', 'api', 'user', 'util'], function ($, api, user, util) {
    var vm,
            controller = {
                init: function () {
                	if(user.isLogin()){
				        window.location.hash="#/";
				        return;
				    }	
                	
                    api.initFooter(3);
                    api.setTitle('找回登录密码');
                    if (!vm) {
                        this.render();
                    } else {
                        vm.show = true;
                    }
                },
                render: function () {
                    var that = this;
                    $('#pages').append('<div id="getLogin"></div>');
                    require(['text!pages/pass/getlogin.html', 'Vue'], function (tpl, Vue) {
                        vm = new Vue({
                            el: '#getLogin',
                            data: {
                                show: true,
                                step: 1,
                                form: {
                                    mobile: '',
                                    verifycode: '', // 手机短信码
                                    captcha: '', // 图片验证码
                                    loginpwd: '',
                                    reloginpwd: ''
                                },
                                wxid:false,
                                imgSrc: false
                            },
                            methods: {
                                checkMobile: function () {
                                    if (this.form.verifycode == '') {
                                        util.msg('请输入验证码！');
                                        return false;
                                    }

                                    api.pwdCheck({smcode: this.form.verifycode}, function (rs) {
                                        if (rs.retcode == '0') {
                                            controller.loadWxid();
                                        } else {
                                            util.dealErrorCode(rs.retcode);// 20161209dyx:增加提示信息
                                        }
                                    });
                                },
                                getVerify: function () { // 获取手机短信码
                                    if (this.form.captcha == '') {
                                        util.msg('请先输入动态码！');
                                    } else {
                                        util.Verify.send(this.form.mobile, this.form.captcha, $('#btnVerify'));
                                    }
                                },
                                resetPwd: function () {
                                    var error = false,
                                        btn=$('#subBtn');
                                    if (typeof this.form.loginpwd == 'undefined' || this.form.loginpwd == '') {
                                        error = '请输入新密码';
                                    } else if (typeof this.form.reloginpwd == 'undefined' || this.form.reloginpwd == '') {
                                        error = '请确认新登陆密码';
                                    } else if (this.form.loginpwd !== this.form.reloginpwd) {
                                        error = '两次输入密码不一致！'
                                    }else if(this.form.loginpwd.length<6){
                                    	error = '密码必须大于6位'
                                    }
                                    if (error) {
                                        util.msg(error);
                                    } else {
                                        var params = {mobile:this.form.mobile, pwd:this.form.loginpwd, pwdtype: 49,weixinid:this.wxid};
                                        btn.val("提交中……").attr("disabled", "true");
                                        api.resetPwd({
                                         error:function(){
                                             btn.removeAttr("disabled").val("确认修改"); //启用按钮
                                         },
                                         data:params,
                                         success:function(rs){
                                         	btn.removeAttr("disabled").val("确认修改"); //启用按钮
                                            if(rs.retcode=='0'){
                                                util.msg('重置成功！');
                                                setTimeout(function(){
                                                    vm.step = 1;
                                                },1);
                                                location.hash = '#/';
                                            }else if(rs.retcode=='43'){
	                                        	 	 vm.step = 1;
	                                        	     util.dealErrorCode(rs.retcode);
                                            }else{
                                                util.dealErrorCode(rs.retcode);
                                            }
                                        }    
                                        })
                                    }
                                },
                                changeCaptcha: function () {
                                    that.loadCaptcha();
                                }
                            },
                            template: tpl
                        });
                        that.loadCaptcha();//20161210dyx
                    })
                },
                loadWxid:function(){
                	if(util.isWxBrowser()){
                		vm.wxid=api.getWxid();
                		vm.step=2;
                		return;
                	}
                	api.mobileToWxid({
                	    data:{mobile:vm.form.mobile},
                	    success:function(rs){
                	        if(rs.retcode=='0'){
                	        	if(rs.data.wxid!==''){
                	        	  vm.wxid=rs.data.wxid;
                	              vm.step=2;
                	        	}else if(rs.recode=='43'){
                        	 	 vm.step = 1;
                        	     util.dealErrorCode(rs.retcode);
                        	    }else{
                	        	   util.msg('帐号不存在！');
                	        	}
                	        }
                	    }
                	})
                },
                loadCaptcha: function (){
                    api.captcha(function (rs) {
                        if (rs.retcode == '0') {
                            vm.imgSrc = 'data:image/png;base64,' + rs.imgcode;
                        }
                    })
                },
                onRouteChange: function () {
                    if (vm)
                        vm.show = false;
                }
            };
    return controller;
})